package com.njh.charges.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.hos.entity.his.*;
import com.hos.entity.stock.Medicines;
import com.njh.charges.feigns.MedicinesFeign;
import com.njh.charges.mapper.CareHistoryMapper;
import com.njh.charges.mapper.CareOrderItemMapper;
import com.njh.charges.mapper.CareOrderMapper;
import com.njh.charges.mapper.CareOrderVoMapper;
import com.njh.charges.service.HandleMedicineService;
import com.njh.charges.vo.CareOrderVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：njh
 * @date ：Created in 2023/12/15 17:27
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class HandleMedicineServiceImpl implements HandleMedicineService {

    @Resource
    private CareHistoryMapper careHistoryMapper;
    @Resource
    private CareOrderVoMapper careOrderVoMapper;
    @Resource
    private CareOrderItemMapper careOrderItemMapper;
    @Resource
    private MedicinesFeign medicinesFeign;

    @Override
    public Map getChargedCareHistoryOnlyMedicinesByRegId(String regId) {

        //查病历表
        LambdaQueryWrapper<CareHistory> wrapper = new LambdaQueryWrapper<>();
        // 进行条件查询
        if (StringUtils.hasText(regId)) {
            wrapper.eq(CareHistory::getRegId, regId);
        }
        CareHistory careHistory = this.careHistoryMapper.selectOne(wrapper);


        //根据ch_id查处方表获取co_id
        LambdaQueryWrapper<CareOrderVo> wrapper2 = new LambdaQueryWrapper<>();
        // 进行条件查询
        if (StringUtils.hasText(careHistory.getChId())) {
            wrapper2.eq(CareOrderVo::getChId, careHistory.getChId());
            wrapper2.eq(CareOrderVo::getCoType, 0);
        }
        List<CareOrderVo> careOrderVos = this.careOrderVoMapper.selectList(wrapper2);

        //根据coid查询care_order_item
        LambdaQueryWrapper<CareOrderItem> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(CareOrderItem::getCoId, careOrderVos.get(0).getCoId())
                .eq(CareOrderItem::getStatus,1);
        List<CareOrderItem> careOrderItems = careOrderItemMapper.selectList(wrapper3);
        careOrderVos.get(0).setCareOrderItems(careOrderItems);


//        List l=new ArrayList();
//
//        for (int i=0;i<careOrders.size();i++) {
//            //根据co_id查处方详情表
//            LambdaQueryWrapper<CareOrderItem> wrapper3 = new LambdaQueryWrapper<>();
//            // 进行条件查询
//            if (StringUtils.hasText(careOrders.get(i).getCoId())) {
//                wrapper3.eq(CareOrderItem::getCoId, careOrders.get(i).getCoId());
//            }
//            wrapper3.eq(CareOrderItem::getStatus,1);
//            wrapper3.eq(CareOrderItem::getItemType,0);
//            List<CareOrderItem> careOrderItems = this.careOrderItemMapper.selectList(wrapper3);
//            if(careOrderItems.isEmpty()){
//                l.add(i);
//                continue;
//            }
//            careOrders.get(i).setCareOrderItems(careOrderItems);
//        }
//
//        //删除无需发药的careOrders列表
//        for (int i = l.size()-1; i >=0; i--) {
//            careOrders.remove(i);
//        }


        Map map = new HashMap();
        map.put("careHistory", careHistory);
        map.put("careOrders", careOrderVos);

        return map;
    }

    @Override
    public Integer doMedicine(List<String> list) {


//         进行减少库存
        QueryWrapper<CareOrderItem> wrapper1=new QueryWrapper();
        wrapper1.lambda().in(CareOrderItem::getItemId,list);
        List<CareOrderItem> careOrderItems = careOrderItemMapper.selectList(wrapper1);

        for (CareOrderItem orderItem:careOrderItems){
            Medicines medicines = new Medicines();
            int num = orderItem.getNum().intValue();
            medicines.setMedicinesStockNum(num);
            medicines.setMedicinesId(Long.valueOf(orderItem.getItemRefId()));
            boolean b = medicinesFeign.reducePharmaceuticalById(medicines);

            //减少库存成功则修改状态
            if(b){
                UpdateWrapper wrapper=new UpdateWrapper();
                //根据item_id修改处方详情表状态完成发药
                wrapper.eq("item_id",orderItem.getItemId());
                wrapper.set("status",3);
                careOrderItemMapper.update(wrapper);
            }
        }

        return 0;
    }

    @Override
    public Map getNoChargeCareHistoryByRegId(String regId) {

        //查病历表
        LambdaQueryWrapper<CareHistory> wrapper = new LambdaQueryWrapper<>();
        // 进行条件查询
        if (StringUtils.hasText(regId)) {
            wrapper.eq(CareHistory::getRegId, regId);
        }
        CareHistory careHistory = this.careHistoryMapper.selectOne(wrapper);


        //根据ch_id查处方表获取co_id
        LambdaQueryWrapper<CareOrderVo> wrapper2 = new LambdaQueryWrapper<>();
        // 进行条件查询
        if(careHistory!=null){
            if (StringUtils.hasText(careHistory.getChId())) {
                wrapper2.eq(CareOrderVo::getChId, careHistory.getChId());
            }
        }
        List<CareOrderVo> careOrders = this.careOrderVoMapper.selectList(wrapper2);

        List l=new ArrayList();

        for (int i=0;i<careOrders.size();i++) {
            //根据co_id查处方详情表
            LambdaQueryWrapper<CareOrderItem> wrapper3 = new LambdaQueryWrapper<>();
            // 进行条件查询
            if (StringUtils.hasText(careOrders.get(i).getCoId())) {
                wrapper3.eq(CareOrderItem::getCoId, careOrders.get(i).getCoId());
            }
            wrapper3.eq(CareOrderItem::getStatus,0);
            List<CareOrderItem> careOrderItems = this.careOrderItemMapper.selectList(wrapper3);
            if(careOrderItems.isEmpty()){
                l.add(i);
                continue;
            }
            careOrders.get(i).setCareOrderItems(careOrderItems);
        }

        //删除无需发药的careOrders列表
        for (int i = l.size()-1; i >=0; i--) {
            careOrders.remove(i);
        }


        Map map = new HashMap();
        map.put("careHistory", careHistory);
        map.put("careOrders", careOrders);

        return map;
    }
}